<template>
  <div id="updateStaff">
    <el-dialog
        title="修改员工信息"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="closeBtn"
    >
      <el-form ref="formRef" :rules="formRules" :model="userInfo" label-width="80px">
        <el-form-item label="头像">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/backUser/avatarImgUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item prop="username" label="用户名">
          <el-input v-model="userInfo.username"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="userInfo.password"></el-input>
        </el-form-item>
        <el-form-item prop="name" label="姓名">
          <el-input v-model="userInfo.name"></el-input>
        </el-form-item>
        <el-form-item prop="phone" label="手机">
          <el-input v-model="userInfo.phone"></el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="closeBtn">取 消</el-button>
    <el-button type="primary" @click="updateStaffBtn('formRef')">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "UpdateStaff",
  data() {
    return {
      //头像地址
      imageUrl: "",
      //用户id
      userId: "",
      //用户信息
      userInfo:{},

      dialogVisible: true,

      // 表单校验规则
      formRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur'},
          { min: 5, max: 8, message: '请输入5~8个字符', trigger: 'blur'}
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur'},
          { min: 6, max: 10, message: '请输入6~10个字符', trigger: 'blur'}
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur'},
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    //修改
    updateStaffBtn(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('是否确认修改？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async () => {
            //确定
            const { data: res } = await this.$http.post(`/backUser/updateBackUser`,this.userInfo);
            this.$message.success(res);
            //自定义事件处理界面刷新
            this.$emit('query');
            this.$router.push('/staff');
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            });
          });
        }
      })
    },
    //头像成功上传的回调
    handleAvatarSuccess(res, file) {
      this.imageUrl = res;
      this.userInfo.avatarImg = this.imageUrl;
    },
    //上传之前的校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //关闭
    closeBtn() {
      this.$router.push('/staff');
    },
    //回显当前用户信息
    async queryUser() {
      const { data: res } = await this.$http.post(`/backUser/queryBackUser/${this.userId}`);
      this.userInfo = res
      this.imageUrl = this.userInfo.avatarImg
    }
  },
  created() {
    //用户id
    this.userId = this.$route.query.id;
    //回显当前用户信息
    this.queryUser();
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  line-height: 78px;
  text-align: center;
}
.avatar {
  width: 78px;
  height: 78px;
  display: block;
}
</style>